<?php
class newYearModel extends appModel{
	public function getVote()
	{
// 		$sql = "SELECT COUNT(*) FROM ".$this->table("newyear_vote")." WHERE vote =1 ";
// 		$result['first'] = $this->getOne($sql);
// 		$sql = "SELECT COUNT(*) FROM ".$this->table("newyear_vote")." WHERE vote =2 ";
// 		$result['second'] = $this->getOne($sql);
		$sql = 'SELECT first,second FROM '.$this->table("newyear_vote_all")." WHERE id = 1 ";
		$result = $this->getRow($sql);
		return $result;
	}
	
	public function getVoteAndWXInfo($id)
	{
		if(!$id)
		{
			return false;
		}
		$sql = "SELECT wxid,openid,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
		$result=$this->getRow($sql);
		
		if($result)
		{
			$sql = "SELECT vote FROM ".$this->table('newyear_vote')." WHERE wxid = $id LIMIT 1 ";
			$vote = $this->getOne($sql);
			$result['vote'] = $vote;
			return $result;
		}
		else
		{
			return false;
		}
	}
	 
	public function sendVote($info)
	{
		$sql = "SELECT id FROM ".$this->table('newyear_vote')." WHERE wxid = ".$info['wxid']." LIMIT 1 ";
		$id = $this->getOne($sql);
		if($id){
			$result = $this->update($info, array("table"=>"newyear_vote","id"=>$id));
			if($result){
				if($info['vote']==1){
					$sql = "UPDATE ".$this->table("newyear_vote_all")." SET first = first+1 ,second = second-1 WHERE id =1 ";
				}else{
					$sql = "UPDATE ".$this->table("newyear_vote_all")." SET first = first-1 ,second = second+1 WHERE id =1 ";
				}
				$this->query($sql);
				$res['status'] = "update";
			}else{
				return $result;
			}
		}else{//新建
			$result = $this->insert($info, "newyear_vote");
			if($result){
				if($info['vote']==1){
					$sql = "UPDATE ".$this->table("newyear_vote_all")." SET first = first+1 WHERE id =1 ";
				}else{
					$sql = "UPDATE ".$this->table("newyear_vote_all")." SET second = second+1 WHERE id =1 ";
				}
				$this->query($sql);
				$res['status'] = "insert";
			}else{
				return $result;
			}
		}
		if($res){
			$vote = $this->getVote();
			$res['vote'] =$vote;
		}
		return $res;
	}
	
	public function getNewYearZBActivity()
	{
		$sql = "SELECT begin_time,end_time,zbhb_id,status FROM ".$this->table("newyear_activity")." ORDER BY status ASC ";
		$result = $this->getAll($sql);
		foreach ($result as $k=>$v){//加上10秒倒计时
			$result[$k]['end_time'] = $v['end_time']+10;
		}
		return $result;
	}
	
	public function getZBLottery($id){
		$sql = "SELECT lottery_id FROM ".$this->table('zbhb_info')." WHERE id = $id ";
		$result =  $this->getOne($sql);
		return $result;
	}
	public function getTVLottery($id){
		$sql = "SELECT lottery_id FROM ".$this->table('lucky_info')." WHERE id = $id";
		$result =  $this->getOne($sql);
		return $result;
	}
	public function getNewYearTVActivity()
	{
		$sql = "SELECT begin_time,end_time,tvhb_id,status FROM ".$this->table("newyear_activity")." ORDER BY status ASC ";
		$result = $this->getAll($sql);
		foreach ($result as $k=>$v){//加上10秒倒计时
			$result[$k]['end_time'] = $v['end_time']+30;
		}
		return $result;
	}
	public function isVote($wxid)
	{
		$sql = "SELECT id FROM ".$this->table('newyear_vote')." WHERE wxid = $wxid LIMIT 1 ";
		$id = $this->getOne($sql);
		return $id;
	}
	
	public function isGetHB($status)
	{
		$time = yf_time();
		$sql = "SELECT ID FROM ".$this->table("newyear_activity")." WHERE status=$status AND tvbegin_time<=$time AND tvend_time>=$time LIMIT 1 ";
		return $this->getOne($sql);
	}
}